package com.test.dao.impl;


import com.test.dao.CustomerDao;
import com.test.domain.Customer;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Date;
import java.sql.Types;
import java.util.List;


@Repository
public class CustomerDaoImpl implements CustomerDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public Customer getCustomerByPhoneNumber(String phoneNumber) {
        String sql = "select * from customer where customer_phone_number=?";
        Customer customer=jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Customer>(Customer.class),phoneNumber);
        System.out.println(customer);
        return customer;
    }

    //用于登录的操作
    @Override
    public int registerAndLogin(int C_M, String phoneNumber, String password) {

        if (phoneNumber=="" || password==""){
            return 3;
        }
        else {
            Object execute_test = jdbcTemplate.execute("execute register_and_login ?,?,?,? ",
                    (CallableStatementCallback) cs -> {
                        cs.setObject("C_M",C_M);
                        cs.setObject("phone_number",phoneNumber);
                        cs.setObject("password",password);
                        cs.setObject("status",-1);
                        cs.registerOutParameter("status", Types.INTEGER);
                        cs.execute();
                        Object ksbm = cs.getObject("status");
                        return ksbm;
                    });
            System.out.println(execute_test);
            return (int)execute_test;
        }




    }

    @Override
    public void DetermineMembershipLevel(String username) {
        Object execute_test = jdbcTemplate.execute("execute DetermineMembershipLevel ?,? ",
                (CallableStatementCallback) cs -> {
                    cs.setObject("customerUsername",username);
                    cs.setObject("status",-1);
                    cs.registerOutParameter("status", Types.INTEGER);
                    cs.execute();
                    Object ksbm = cs.getObject("status");
                    return ksbm;
                });
        System.out.println(execute_test);
    }

    @Override
    public List<String> getUserName() {
        String sql="select distinct customer_username  from customer";
        return jdbcTemplate.queryForList(sql,String.class);
    }

    @Override
    public List<Customer> getCustomerList() {
        String sql="select *  from customer";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Customer.class));
    }

    @Override
    public int judge(String phoneNumber) {
        Object execute_test = jdbcTemplate.execute("execute judge ?,? ",
                (CallableStatementCallback) cs -> {
                    cs.setObject("CustomerTel",phoneNumber);
                    cs.setObject("status",-1);
                    cs.registerOutParameter("status", Types.INTEGER);
                    cs.execute();
                    Object ksbm = cs.getObject("status");
                    return ksbm;
                });
        System.out.println(execute_test);
        return (int) execute_test;
    }

    @Override
    public void updateForbiddenTimeByPhoneNumber(Date date, String employeeNumber, String phoneNumber) {
        String sql="update customer set forbidden_time=?,employee_number=? where customer_phone_number=?";
        jdbcTemplate.update(sql,date,employeeNumber,phoneNumber);
    }


}
